package com.ymttest.business.data;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import com.google.gson.JsonObject;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.AddressRequestDtoBean;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.utils.TestDataManager;

public class AddAddress {
	public static void AddAddressAndAccounting() throws Exception {
		UserRegisterInfoRequestDtoBean urb = JUserCallService.initUserRegisterInfoRequestDtoBean();
		// urb.setDeviceId(TestDataManager.getRandomUUID().toString());
		UserRegisterInfoRequestDtoCall uc = JUserCallService.UserRegisterInfoRequestDtoCall(urb);
		Integer uid = uc.getUserid();
		String token=uc.getAccessToken();
		// 添加地址
		AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(uid);
		String addressid =JUserCallService.AddressRequestDtoCall(addressBean);
		// 设置账户余额
		String aid = AccountCallServiceV2.getAccountId(uid);
		AccountCallServiceV2.resetAccount(aid, new BigDecimal("1000"), new BigDecimal("100"));
		TestDataManager.appendFileToLine("newToken4.txt", token + "," + uid + "," + addressid+","+aid);
	}

	public static void BatchAddAddress() throws Exception {

		ExecutorService SERVICE = Executors.newFixedThreadPool(20);
		ArrayList<String> list = TestDataManager.getLines("newToken.txt");
		for (String s : list) {
			SERVICE.execute(() -> {
				UserRegisterInfoRequestDtoBean regbean;
				try {
					Integer uid = Integer.valueOf(s.split(",")[1]);
					String token = s.split(",")[0];
					// 注册用户
					AddressRequestDtoBean addressBean = JUserCallService.initAddressBean(uid);
					addressBean.setbIsDefault(true);
					String aid = JUserCallService.AddressRequestDtoCall(addressBean);
					TestDataManager.appendFileToLine("newToken4.txt", token + "," + uid + "," + aid);
				} catch (Exception e) {
					e.printStackTrace();
				}
			});
		}
		SERVICE.shutdown();
		while (!SERVICE.isTerminated()) {
			Logger.debug("not isTerminated");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		Logger.debug("isTerminated");
	}

	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 100; i++) {
			AddAddress.AddAddressAndAccounting();
		}
	}
}
